<?php
function getAccessToken()
{
    //检测文件是否存在
    $file = 'client_token.txt';
    if (file_exists($file)) {
        //获取文件的最后修改时间
        $mtime = filemtime($file);
        //获取当前时间
        $now = time();
        //判断是否过期
        if (($now - $mtime) < 300) {
            //未过期，直接读取文件内容
            $access_token = file_get_contents($file);
            return $access_token;
        } else {
            $access_token = get_access_token();
            file_put_contents($file, $access_token);
            return $access_token;

        }
    } else {
        $access_token = get_access_token();
        file_put_contents($file, $access_token);
        return $access_token;
    }
}
function get_access_token()
{
    $access_token = json_decode(http_post("https://open.douyin.com/oauth/client_token/", [
        "client_key" => "ttdd96ab2b42a07b6d01",
        "grant_type" => "client_credential",
        "client_secret" => "028db66bdff3fa44cad6f2fafa1faa3d6571b3b4"
    ]), true)['data']['access_token'];
    return $access_token;
}
function http_post($url, $data)
{
    $postdata = json_encode($data);
    // echo ($postdata);
    // exit;

    $options = array(
        'http' => array(
            'method' => 'POST',
            'header' => 'Content-type:application/json',
            'content' => $postdata,
            'timeout' => 15 * 60
        )
    );
    $context = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
    return $result;
}
function post($url, $data)
{
   
    // echo json_encode($data);
    // exit;
   
    $curl = curl_init();
    curl_setopt_array($curl, [
        CURLOPT_URL => $url ,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => json_encode($data,JSON_UNESCAPED_UNICODE),
        CURLOPT_HTTPHEADER => [
            "Content-Type: application/json",
            "access-token: " . getAccessToken(),
        ],
    ]);

    $response = curl_exec($curl);
    $err = curl_error($curl);
    curl_close($curl);
    if ($err) {
        return "cURL Error #:" . $err;
    } else {
        return $response;
    }
}
function get($url, $data)
{
    // var_dump($url . http_build_query($data));exit;
    $curl = curl_init();
    curl_setopt_array($curl, [
        CURLOPT_URL => $url . '?' . http_build_query($data),
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "GET",
        CURLOPT_POSTFIELDS => json_encode($data),
        CURLOPT_HTTPHEADER => [
            "Content-Type: application/json",
            "access-token: " . getAccessToken(),
        ],
    ]);

    $response = curl_exec($curl);
    $err = curl_error($curl);
    curl_close($curl);
    if ($err) {
        return "cURL Error #:" . $err;
    } else {
        return $response;
    }
}